package com.example.drawtexttobitmap;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.annotation.IntDef;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by Alex-Zhang on 2017/11/26.
 */

public class DrawText {
    private int defaultWidth;
    private int defaultHeight;
    private final Canvas mCanvas;
    private int textColor = Color.RED;
    private int textSize = 20;
    private Typeface typeface = Typeface.MONOSPACE;
    private int bgColor = Color.WHITE;
    private String title = "我只主标题";
    private String subTitle = "我只副标题";
    private final TextPaint textPaint;
    private Layout.Alignment alignment = Layout.Alignment.ALIGN_OPPOSITE;
    private Bitmap bitmap;
    private int SPACE;
    public static final int TOP = 0;
    public final static int CENTER = 2;
    private float offsetY;

    @IntDef({TOP, CENTER})
    @Retention(RetentionPolicy.SOURCE)

    public @interface Align {
    }

    @Align
    private int defaultAlign = TOP;

    public DrawText() {
        defaultWidth = 200;
        defaultHeight = 500;
        bitmap = Bitmap.createBitmap(defaultWidth, defaultHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(bitmap);
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
        textPaint.setTypeface(typeface);
    }

    public void setBitmapWidthAndHeight(int width, int height) {
        this.defaultWidth = width;
        this.defaultHeight = height;
        textCreateBitmap();
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
        textCreateBitmap();
    }

    public void setAlignment(Layout.Alignment alignment) {
        this.alignment = alignment;
        textCreateBitmap();
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        textPaint.setTextSize(textSize);
        textCreateBitmap();
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        textCreateBitmap();
    }

    public void setSpace(int space) {
        this.SPACE = space;
        textCreateBitmap();
    }

    public void setTypeface(Typeface typeface) {
        this.typeface = typeface;
        textCreateBitmap();
    }

    private StaticLayout[] measureText() {
        StaticLayout[] staticLayouts = new StaticLayout[2];
        if (title != null) {
            StaticLayout staticLayoutTitle = new StaticLayout(title, textPaint, defaultWidth, alignment, 1.0f, 0, false);
            staticLayouts[0] = staticLayoutTitle;
        } else {
            staticLayouts[0] = null;
        }
        if (subTitle == null) {
            StaticLayout staticLayoutSubTitle = new StaticLayout(subTitle, textPaint, defaultWidth, alignment, 1.0f, 0, false);
            staticLayouts[1] = staticLayoutSubTitle;
        } else {
            staticLayouts[1] = null;
        }

        return staticLayouts;
    }

    public Bitmap textCreateBitmap() {
        StaticLayout[] staticLayouts = measureText();
        mCanvas.save();
        mCanvas.drawColor(bgColor);
        mCanvas.translate(0, offsetY);
        if (staticLayouts[0] != null) {
            staticLayouts[0].draw(mCanvas);
        }
        mCanvas.restore();
        mCanvas.drawBitmap(bitmap, 0, 0, null);
        mCanvas.save();
        mCanvas.translate(0, offsetY + staticLayouts[0].getHeight() + SPACE);
        if (staticLayouts[1] != null) {
            staticLayouts[1].draw(mCanvas);
        }
        mCanvas.restore();
        mCanvas.drawBitmap(bitmap, 0, 0, null);
        return bitmap;
    }


    public void setAlign(@Align int alignType) {
        this.defaultAlign = alignType;
        if (measureText()[0] != null && measureText()[1] != null) {
            float height = measureText()[0].getHeight() + measureText()[1].getHeight() +SPACE;
            float dy = (float) defaultHeight / 2 - height / 2;
            if (alignType == CENTER) {
                offsetY = dy;
            } else {
                offsetY = 0;
            }
        }
        textCreateBitmap();
    }

    @Align
    public int getAlign() {
        return defaultAlign;
    }
}
